package com.jp.knowledge.my.model;

import com.google.gson.JsonObject;
import com.jp.knowledge.model.UserData;
import com.jp.knowledge.util.JsonUtil;
import com.jp.knowledge.util.L;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/2/14.
 */

public class SettingUserDataModel implements Serializable {

    private String openid;
    private String username;
    private String nickname;
    private String gender;
    private String portrait;
    private String companyName;
    private String departmentName;
    private String position;
    private String departmentId;
    private String email;
    private int skillLevel;
    private List<UserData.SkillsBean> skills;
    private List<String> addSkillIds;
    private List<String> delSkillIds;
    private List<UserData.IndustryBean> industry;
    private List<String> addIndustryIds;
    private List<String> delIndustryIds;

    public SettingUserDataModel(){}

    public SettingUserDataModel(UserData userData){
        getUserSettingDataModel(userData);
    }

    public String getOpenid() {
        return openid;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getPortrait() {
        return portrait;
    }

    public void setPortrait(String portrait) {
        this.portrait = portrait;
    }

    public String getPosition() {
        return position;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public String getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(String departmentId) {
        this.departmentId = departmentId;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getSkillLevel() {
        return skillLevel;
    }

    public void setSkillLevel(int skillLevel) {
        this.skillLevel = skillLevel;
    }

    public List<UserData.SkillsBean> getSkills() {
        return skills;
    }

    public void setSkills(List<UserData.SkillsBean> skills) {
        this.skills = skills;
    }

    public List<String> getAddSkillIds() {
        return addSkillIds;
    }

    public void setAddSkillIds(List<String> addSkillIds) {
        this.addSkillIds = addSkillIds;
    }

    public List<String> getDelSkillIds() {
        return delSkillIds;
    }

    public void setDelSkillIds(List<String> delSkillIds) {
        this.delSkillIds = delSkillIds;
    }

    public List<UserData.IndustryBean> getIndustry() {
        return industry;
    }

    public void setIndustry(List<UserData.IndustryBean> industry) {
        this.industry = industry;
    }

    public List<String> getAddIndustryIds() {
        return addIndustryIds;
    }

    public void setAddIndustryIds(List<String> addIndustryIds) {
        this.addIndustryIds = addIndustryIds;
    }

    public List<String> getDelIndustryIds() {
        return delIndustryIds;
    }

    public void setDelIndustryIds(List<String> delIndustryIds) {
        this.delIndustryIds = delIndustryIds;
    }

    public String getSkillText(){
        if(skillLevel == 1)
            return "初级";
        else if(skillLevel == 2)
            return "中级";
        else if(skillLevel == 3)
            return "高级";
        else
            return "未设置";
    }

    public SettingUserDataModel getUserSettingDataModel(UserData userData){
        if(userData == null)
            return this;
        setUsername(userData.getUsername());
        setNickname(userData.getNickname());
        setGender(userData.getGender());
        setPortrait(userData.getPortrait());
        setCompanyName(userData.getCompanyName());
        setDepartmentName(userData.getDepartment());
        setPosition(userData.getPosition());
        setDepartmentId(userData.getDepartmentId());
//        setUserIntro(userData.getUserIntro());
        setEmail(userData.getEmail());
        setSkillLevel(userData.getSkillLevel());
        setSkills(userData.getSkills());
        setIndustry(userData.getIndustry());
        return this;
    }

    public boolean updateSettingDataToUserData(UserData userData){
        if(userData == null)
            return false;
        userData.setUsername(getUsername());
        userData.setNickname(getNickname());
        userData.setNickname(getNickname());
        userData.setGender(getGender());
        userData.setPortrait(getPortrait());
        userData.setCompanyName(getCompanyName());
        userData.setDepartment(getDepartmentName());
        userData.setPosition(getPosition());
        userData.setDepartmentId(getDepartmentId());
//        userData.setUserIntro(getUserIntro());
        userData.setEmail(getEmail());
        userData.setSkillLevel(getSkillLevel());
        userData.setSkills(getSkills());
        userData.setIndustry(getIndustry());
        return true;
    }

    public JsonObject toJsonObject(UserData userData){
        try {
            // 得到删除的标签 ID
            List<String> deleteSkillIds = getDeleteSkillIds(userData);
            if(deleteSkillIds.size() > 0)
                setDelSkillIds(deleteSkillIds);
            // 得到新增的标签 ID
            List<String> addSkillIds = getAddSkillIds(userData);
            if(addSkillIds.size() > 0)
                setAddSkillIds(addSkillIds);

            JsonObject jsonObject = JsonUtil.getInstance().entityToJson(this);
            jsonObject.remove("companyName");
            jsonObject.remove("departmentName");
            jsonObject.remove("skills");
            jsonObject.remove("industry");
            return jsonObject;
        } catch (Exception e){
            return null;
        }
    }

    public boolean hasDataChange(UserData userData){
        if(userData == null)
            return false;
        if((getOpenid() != null && !getOpenid().equals(userData.getOpenid()))
                || (getUsername() != null && !getUsername().equals(userData.getUsername()))
                || (getNickname() != null && !getNickname().equals(userData.getNickname()))
                || (getGender() != null && !getGender().equals(userData.getGender()))
                || (getPortrait() != null && !getPortrait().equals(userData.getPortrait()))
                || (getPosition() != null && !getPosition().equals(userData.getPosition()))
                || (getDepartmentId() != null && !getDepartmentId().equals(userData.getDepartmentId()))
//                || (getUserIntro() != null && !getUserIntro().equals(userData.getUserIntro()))
                || (getEmail() != null && !getEmail().equals(userData.getEmail()))
                || skillLevel != userData.getSkillLevel()
                || getDeleteSkillIds(userData).size() > 0 || getAddSkillIds(userData).size() > 0 ){
            return true;
        }
        return false;
    }

    // 得到删除的标签 ID
    private List<String> getDeleteSkillIds(UserData userData){
        List<String> deleteSkillIds = new ArrayList<>();
        try {
            List<UserData.SkillsBean> userSkillBeans = new ArrayList<>();
            userSkillBeans.addAll(userData.getSkills());
            for (UserData.SkillsBean skillsBean : getSkills()) {
                for (UserData.SkillsBean userSkillBean : userSkillBeans) {
                    if (skillsBean.getSkillId().equals(userSkillBean.getSkillId())) {
                        userSkillBeans.remove(userSkillBean);
                        break;
                    }
                }
            }
            for (UserData.SkillsBean deleteSkill : userSkillBeans)
                deleteSkillIds.add(deleteSkill.getSkillId());
        } catch (Exception e){
            L.e(e.toString());
        }
        return deleteSkillIds;
    }

    // 得到新增的标签 ID
    private List<String> getAddSkillIds(UserData userData){
        List<String> addSkillIds = new ArrayList<>();
        try {
            List<UserData.SkillsBean> addSkills = new ArrayList<>();
            addSkills.addAll(getSkills());
            for (UserData.SkillsBean userSkillBean : userData.getSkills()) {
                for (UserData.SkillsBean skillsBean : addSkills) {
                    if (userSkillBean.getSkillId().equals(skillsBean.getSkillId())) {
                        addSkills.remove(skillsBean);
                        break;
                    }
                }
            }
            for (UserData.SkillsBean addSkill : addSkills)
                addSkillIds.add(addSkill.getSkillId());
        } catch (Exception e){
            L.e(e.toString());
        }
        return addSkillIds;
    }
}
